package ${packgeDO.serviceImplPackageName};

import java.util.List;

<#if bean_is_auto_wire?string=="1">
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
</#if>
<#assign DAOName = "${domain.className}Mapper">
<#assign lower_dao_name = "${domain.className?uncap_first}Mapper">
<#assign param="${domain.className?uncap_first}">
import ${packgeDO.daoPackageName}.${DAOName};
import ${domain.packageName}.${domain.className};
import ${packgeDO.servicePackageName}.I${domain.className}Service;
import net.newfirst.common.core.page.Page;
import org.apache.commons.collections.CollectionUtils;

<#if bean_is_auto_wire?string=="1">
@Service
</#if>
public class ${domain.className}ServiceImpl implements I${domain.className}Service{

	<#if bean_is_auto_wire?string=="1">@Autowired</#if>
	private ${DAOName} ${lower_dao_name};

	<#if bean_is_auto_wire?string=="0">
	public void set${DAOName}(${DAOName} ${lower_dao_name}) {
		this.${lower_dao_name} = ${lower_dao_name};
	}

	</#if>
	
	@Override
	public int save(${domain.className} ${param}) {
		if(null == ${param}) {
			return 0;
		}
		return this.${lower_dao_name}.insert(${param});
	}

	@Override
	public int updateDynamic(${domain.className} ${param}){
		if(null == ${param}) {
			return 0;
		}
		return this.${lower_dao_name}.updateDynamic(${param});
	}

	@Override
	public int deleteById(${tableDO.primaryColumnType} id){
		if(null == id) {
			return 0;
		}
		return this.${lower_dao_name}.deleteByPrimaryKey(id);
	}

	@Override
	public ${domain.className} selectById(${tableDO.primaryColumnType} id) {
		if(null == id) {
			return null;
		}
		return this.${lower_dao_name}.selectByPrimaryKey(id);
	}

	@Override
	public Long selectCountDynamic(${domain.className} ${param}) {
		if(null == ${param}) {
			return 0l;
		}
		return this.${lower_dao_name}.selectCountDynamic(${param});
	}

	@Override
	public List<${domain.className}> selectDynamic(${domain.className} ${param}) {
		if(null == ${param}) {
			return null;
		}
		return this.${lower_dao_name}.selectDynamic(${param});
	}
	

	private List<${domain.className}> selectDynamicPageQuery(${domain.className} ${param}) {
		if(null == ${param}) {
			return null;
		}
		return this.${lower_dao_name}.selectDynamicPageQuery(${param});
	}

	@Override
	public Page<${domain.className}> pageQuery(${domain.className} ${param}) {
	 	if (${param} != null) {
			Long totalCount = this.selectCountDynamic(${param});
			List<${domain.className}> resultList = this.selectDynamicPageQuery(${param});

			Page<${domain.className}> page = new Page<${domain.className}>();
			page.setPageNo(${param}.getPageNo());
			page.setPageSize(${param}.getPageSize());
			page.setTotalCount(totalCount.intValue());
			page.setList(resultList);
			return page;
		}
		return new Page<${domain.className}>();
	}

	@Override
	public Page<${domain.className}> pageQueryByPageNo(${domain.className} ${param},int pageNo,int pageSize) {
		if (${param} != null && pageNo>0 && pageSize>0) {
			${param}.setPageNo(pageNo);
			${param}.setPageSize(pageSize);
			return this.pageQuery(${param});
		}
		return new Page<${domain.className}>();
	}

	@Override
	public void saveForBatch(List<${domain.className}> list) {
		if(CollectionUtils.isEmpty(list)) {
			return;
		}
		this.${lower_dao_name}.saveForBatch(list);
	}
	
	@Override
	public void updateForBatch(List<${domain.className}> list) {
		if(CollectionUtils.isEmpty(list)) {
			return;
		}
		this.${lower_dao_name}.updateForBatch(list);
	}
	
	@Override
	public ${domain.className} selectOne(${domain.className} ${param}) {
		if(null == ${param}) {
			return null;
		}
		return this.${lower_dao_name}.selectOne(${param});
	}

}
